import {HttpVerificationCode} from '../../public/http'
import { ElMessage} from 'element-plus'
let isClick = 1
export const useCode = (email:string,callback:any,tip?:any) => {
	if(isClick === 0){
		return
	}
	isClick = 0
	// 发送验证码
	if(!email){
		ElMessage({
		    message: '请输入邮箱!',
		    type: 'warning',
		})
		isClick = 1
		return;
	}
	const sendCode = (email:string)=>{
		HttpVerificationCode({
			email:email,
			tip:tip
		}).then((res:any)=>{
			if(res.code === 200){
				ElMessage({
				    message: '发送成功!',
				    type: 'success',
				})
				timeoutMain()
				return;
			}
			if(res.code === 403){
				isClick = 1
			}
			ElMessage({
			    message: res.message,
			    type: 'warning',
			})
		})
	}
	sendCode(email)
	const timeoutMain = ()=>{
		let count = 60 as any
		const timeOut = setInterval(()=>{
			if(count > 0){
				count--
			}else{
				clearInterval(timeOut)
				count = "发送验证码"
				isClick = 1
			}
			if(count === "发送验证码"){
				callback(count)
			}else{
				callback(`剩余${count}s`)
			}
			
		},1000)
	}
	
}